package com.hddz.comlib.packet;

import java.io.Serializable;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.hddz.comlib.R;
import com.hddz.comlib.model.Sensor;

public class WeekUpSamplePacket extends WeekUpPacket implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 传感器数据
	 */
	private List<Sensor> pChanels = new ArrayList<Sensor>();

	public List<Sensor> getpChanels() {
		return pChanels;
	}

	public Sensor findSensorByChanel(String chanel) {
		for (Sensor s : pChanels) {
			if (s.getChannel().toLowerCase().equals(chanel.toLowerCase())) {
				return s;
			}
		}
		return new Sensor();
	}

	public void setpChanels(List<Sensor> pChanels) {
		this.pChanels = pChanels;
	}

	@Override
	public String getDataAdd(int resid) {
		// TODO Auto-generated method stub
		String data = "";
		Sensor sensor = null;

		if (resid == R.id.rb_station_item_zxchanel) {
			sensor = findSensorByChanel("P1");
			if (sensor != null) {
				data = String.format("%d", (int) sensor.getValue());
			}
		} else if (resid == R.id.rb_station_item_fxchanel) {
			sensor = findSensorByChanel("P2");
			if (sensor != null) {
				data = String.format("%d", (int) sensor.getValue());
			}
		} else if (resid == R.id.rb_station_item_sjchannel) {
			sensor = findSensorByChanel("P3");
			if (sensor != null) {
				data = String.format("%d", (int) sensor.getValue());
			}
		} else if (resid == R.id.rb_station_item_schanel) {
			sensor = findSensorByChanel("A1");
			if (sensor != null) {
				data = String.format("%.3f", sensor.getValue());
			}
		} else {
			data = super.getDataAdd(resid);
		}
		return data;
	}

	@Override
	public String createViewText() {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		sb.append("站号:" + stationId);
		sb.append(" 时间:" + dateformat.get().format(sendTime));
		for (Sensor s : pChanels) {
			sb.append(" 通道:" + s.getValue() + s.getChannel());
		}
		sb.append(" 电池电压:" + sensor.getValue() + sensor.getChannel());
		sb.append("\r\n");
		return sb.toString();
	}

	@Override
	public Boolean parse(List<Byte> data, int start, int len) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Boolean parse(ByteBuffer data, int len) {
		// TODO Auto-generated method stub
		// byte[] b = new byte[len];
		int perPosition = data.position();
		try {

			// data.get(b, 0, len);
			// this.sourceText = Utility.bystesToStr(b, 0, len);
			// Log.d("WeekUpSamplePacket_收到=>>", this.sourceText);
			// data.position(perPosition);
			stationId = ((data.get() & 0xff) * 256 + (data.get() & 0xff)) * 256 + (data.get() & 0xff);
			// CLogger.Write("statinon --- " + StationID + " " +
			// this.SourceText);
			sendTime = new Date();
			pChanels = new ArrayList<Sensor>();
			while (data.position() < perPosition + len - 1)// 留一位是电压
			{
				int no_flag = data.get() & 0xff;
				int no = no_flag % 16;
				int flag = no_flag / 16;
				switch (flag) {
				case 1:// 累计流量 P
					sensor = new Sensor();
					sensor.setStationId(stationId);
					sensor.setChannel("P" + no);
					sensor.setValue((data.get() & 0xff) * 1000000 + (data.get() & 0xff) * 10000 + (data.get() & 0xff) * 100 + (data.get() & 0xff));
					pChanels.add(sensor);
					break;
				case 2:// K
					sensor = new Sensor();
					sensor.setStationId(stationId);
					sensor.setChannel("K" + no);
					sensor.setValue((data.get() & 0xff) - 0x30);
					pChanels.add(sensor);
					break;
				case 3:// 模拟量 A
					sensor = new Sensor();
					sensor.setStationId(stationId);
					sensor.setChannel("A" + no);
					sensor.setValue(((data.get() & 0xff) * 256 + (data.get() & 0xff)) / 1000.0);// 模拟量解数据时，除以1000；
					pChanels.add(sensor);
					break;
				case 4:// B
					sensor = new Sensor();
					sensor.setStationId(stationId);
					sensor.setChannel("B" + no);
					sensor.setValue((data.get() & 0xff) - 0x30);
					pChanels.add(sensor);
					break;
				}
			}

			sensor = new Sensor();
			sensor.setStationId(stationId);
			sensor.setChannel("V");
			sensor.setValue(((data.get() & 0xff) * 2) / 100.0);// 电池电压解数据时，先乘以2，再除以100
			this.viewText = this.createViewText();
			// Log.d("WeekUpSamplePacket_收到", this.viewText);
			return true;
		} catch (Exception e) {
			// CLogger.Write("WeekUpPacket Parse Error" + e.Message);
			return false;
		}
	}

}
